package edu.sias.course.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import edu.sias.common.core.domain.model.LoginUser;
import edu.sias.course.domain.dto.CatalogDTO;
import edu.sias.course.domain.entity.CatalogPageQueryParams;
import edu.sias.course.domain.entity.PageResult;
import edu.sias.course.domain.entity.QueryPageBean;
import edu.sias.course.domain.pojo.Catalog;

import edu.sias.course.domain.vo.CatalogVO;
import edu.sias.course.mapper.CatalogMapper;
import edu.sias.course.service.CatalogService;
import edu.sias.common.exception.base.BaseException;
import edu.sias.common.utils.DateUtils;
import edu.sias.common.utils.Threads;
import edu.sias.course.utils.Esinterutils;
import edu.sias.framework.web.service.TokenService;
import edu.sias.question.utils.Esutils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**学科目录服务实现类
 * @Description CatalogServiceImpl
 * @Author songyu
 * @Date 2023-09-03
 */
@Service
public class CatalogServiceImpl implements CatalogService {

    @Resource
    private CatalogMapper catalogMapper;


    /**
     * 添加学科目录
     *
     * @param catalogDTO
     */
    @Override
    public void add(CatalogDTO catalogDTO) throws IOException {

        Catalog catalog = new Catalog();

        BeanUtils.copyProperties(catalogDTO, catalog);
        catalog.setCreateDate(DateUtils.dateTime(new Date()));//系统当前时间
        catalog.setStatus(0);//启用
        catalog.setUserId(Threads.getCurrentId());
        catalogMapper.add(catalog);


    }

    /**
     * 学科目录分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findListByPage(QueryPageBean<CatalogPageQueryParams> queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        //执行查询
        CatalogPageQueryParams queryParams = queryPageBean.getQueryParams();
        List<CatalogVO> catalogList = catalogMapper.findByCondition(queryParams);
        Page<CatalogVO> page = (Page<CatalogVO>) catalogList;

        //封装结果返回
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Autowired
    private Esinterutils esinterutils;

    /**
     * 学科目录删除
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {

        try {
            catalogMapper.delete(id);

        } catch (Exception e) {
            throw new BaseException("有关联的面试题无法删除");
        }
    }

    /**
     * 学科目录更新方法
     *
     * @param catalogDTO
     */
    @Override
    public void update(CatalogDTO catalogDTO) {
        Catalog catalog = new Catalog();
        BeanUtils.copyProperties(catalogDTO, catalog);
        Integer userId = Threads.getCurrentId();
        catalog.setUserId(userId);
        catalogMapper.update(catalog);
    }

/**
 * 根据目录id查询目录
 *
 * @param catalogId
 * @return
 */

    @Override
    public CatalogDTO findById(Integer catalogId) {
       CatalogDTO CatalogDTO  = catalogMapper.findById(catalogId);
        return CatalogDTO;
    }

    }
